//
//  StickersViewController.m
//  Picture
//
//  Created by Killer on 2016/11/25.
//  Copyright © 2016年 孙玉琢. All rights reserved.
//

#import "StickersViewController.h"
#import "ViewController.h"

@interface StickersViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
// 屏幕高度
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 屏幕宽度
#define WIDTH [UIScreen mainScreen].bounds.size.width
@property (nonatomic, strong) UICollectionView * collect;
@property (nonatomic, strong) UIView *headerView;


@end

@implementation StickersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadingViews];
    // Do any additional setup after loading the view.
}
-(void)loadingViews{
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(mainQueue, ^{
        [self setBackImage];
         self.view.backgroundColor =[UIColor blackColor];
    });
    dispatch_async(mainQueue, ^{
        [self headerView];
    });
    dispatch_barrier_async(mainQueue, ^{
         [self loadViews];
    });
}
-(void)setBackImage{
    UIImageView *image = [[UIImageView alloc]initWithImage:self.backImage];
    [image setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [image setContentMode:UIViewContentModeScaleAspectFill];
    image.backgroundColor = [UIColor darkGrayColor];
    image.alpha =0.3;
    [self.view addSubview:image];
}
-(UIView*)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
        _headerView.backgroundColor = [UIColor clearColor];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height-50, WIDTH, 40)];
        lable.textAlignment =NSTextAlignmentCenter;
        if (self.itemTitle) {
            lable.text =self.itemTitle;
        }
        lable.textColor = [UIColor whiteColor];
        [lable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-50, 20, 40, 40)];
        [backButton setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_headerView];
        [_headerView addSubview:lable];
        [_headerView addSubview:backButton];
    }
    return _headerView;
}
-(void)loadViews{
    //创建一个layout布局类
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小为100*100
    layout.itemSize = CGSizeMake(100, 100);
    //创建collectionView 通过一个布局策略layout来创建
    self.collect = [[UICollectionView alloc]initWithFrame:CGRectMake((WIDTH-320)/2, 100, 320,HEIGHT-120) collectionViewLayout:layout];
    self.collect.backgroundColor = [UIColor clearColor];
    //代理设置
    self.collect.delegate=self;
    self.collect.dataSource=self;
    //注册item类型 这里使用系统的类型
    [self.collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
    [self.view addSubview:self.collect];
}
//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}
//返回每个item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
//解决item复用
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)];
    image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArray[indexPath.row]]];
    cell.contentView.layer.borderColor =[[UIColor whiteColor] CGColor];
    cell.contentView.layer.borderWidth = 2;
    [cell.contentView addSubview:image];
    return cell;
}

#pragma mark -UICollectionViewDelegate
//UICollectionView被选中的时候调用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%zd",indexPath.item);
    self.imageBlock(self.imageArray[indexPath.row]);
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
